import { createRouter, createWebHashHistory } from "vue-router";
import { Toast } from "vant";

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/",
      component: () => import("../views/home/WaiMaiHome"),
    },
    {
      path: "/home",
      component: () => import("../views/home/WaiMaiHome"),
    },
    {
      path: "/cart",
      component: () => import("../views/cart/WaiMaiCart"),
      meta: {
        isAuth: true,
      },
    },
    {
      path: "/order",
      component: () => import("../views/order/WaiMaiOrder"),
      meta: {
        isAuth: true,
      },
    },
    {
      path: "/mine",
      component: () => import("../views/mine/WaiMaiMine"),
      meta: {
        isAuth: true,
      },
    },

    {
      path: "/store",
      component: () => import("../views/store/WaiMaiStore"),
    },
    {
      path: "/createorder",
      component: () => import("../views/cart/createOrder/WaiMaiCreateOrder"),
      meta: {
        isAuth: true,
      },
    },
    {
      path: "/address",
      component: () => import("../views/address/WaiMaiAddress"),
      meta: {
        isAuth: true,
      },
    },
    {
      path: "/addressedit",
      component: () => import("../views/addressEdit/WaiMaiAddressEdit"),
      meta: {
        isAuth: true,
      },
    },
    {
      path: "/userinfoedit",
      component: () => import("../views/userInfoEdit/UserInfoEdit"),
      meta: {
        isAuth: true,
      },
    },
    {
      path: "/login",
      component: () => import("../views/login/WaiMaiLogin"),
    },
    {
      path: "/register",
      component: () => import("../views/register/WaiMaiRegister"),
    },

    {
      path: "/mockinterview",
      component: () => import("../views/jsonMockInterview/InterviewTest"),
    },
  ],
});

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.meta.isAuth) {
    // 登录以后，会在localstorage里面存储一个标识
    if (localStorage.isLogin === "login") {
      // 放行
      next();
    } else {
      next("/login");
      Toast("请先去登录");
    }
  } else {
    // 放行
    next();
  }
});

export default router;
